﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Collections;
using System.Web.Services;

namespace Catering.Catering_Page.Kufang
{
    public partial class Kfsz : UserIsNull
    {

        Catering_Rules.StorageRoom CR_Info = new Catering_Rules.StorageRoom();
        Catering_Model.StorageRoom CM_Info = new Catering_Model.StorageRoom();
        Catering.View_Operaction_Common voc = new View_Operaction_Common();
        public static DataTable dt = new Catering_DataSet.RawInfoDataTable();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int companyid = 0;
                if (Session["UserID"] != null)
                {
                    string userid = Session["UserID"].ToString();
                    companyid = new Catering_Rules.T_Personnel().GetCompanyIDByPID(Convert.ToInt32(userid));
                }
                voc.DropDownListDataBind_Database(this.dr_Db, "", "");
                voc.DropDownListDataBind_Group(this.DDL_zuoyequ, companyid);

                if (DDL_zuoyequ.Items.Count > 0)
                {
                    voc.DropDownListDataBind_Class(this.DDL_banzu, DDL_zuoyequ.Items[0].Value);
                }
                this.but_waing_Save.Enabled = false;
                this.LoadData();
            }
        }

        //加载页面数据
        public void LoadData()
        {
            voc.TreeViewDataBing_RawMaterials(this.Tv_RawMaterial, "RAWTID=" + this.dr_Db.SelectedValue);

            DataTable dt_StorageRoom = new Catering_Rules.StorageRoom().GetAllList().Tables[0];
            this.Repeater1.DataSource = dt_StorageRoom;
            this.Repeater1.DataBind();
        }

        //选择库房
        protected void dr_Db_SelectedIndexChanged(object sender, EventArgs e)
        {
            voc.TreeViewDataBing_RawMaterials(this.Tv_RawMaterial, "RAWTID=" + this.dr_Db.SelectedValue);
        }

        //根据作业区加载班组
        protected void DDL_zuoyequ_SelectedIndexChanged(object sender, EventArgs e)
        {
            voc.DropDownListDataBind_Class(this.DDL_banzu, DDL_zuoyequ.SelectedValue);

            if (this.Tv_RawMaterial.SelectedNode == null || this.Tv_RawMaterial.SelectedNode.Target == "type" || this.Tv_RawMaterial.SelectedNode.Target == "typeinfo")
                return;

            this.LoadJJX();

        }

        //根据作业区加载班组
        protected void DDL_banzu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Tv_RawMaterial.SelectedNode == null || this.Tv_RawMaterial.SelectedNode.Target == "type" || this.Tv_RawMaterial.SelectedNode.Target == "typeinfo")
                return;

            this.LoadJJX();
        }

        //加载警戒线数据
        public void LoadJJX()
        {
            string strRawID = this.Tv_RawMaterial.SelectedValue;

            DataTable dt_RoomWaing = new Catering_Rules.RoomWaing().GetList("RAWID=" + strRawID + " and ClassID=" + this.DDL_banzu.SelectedValue).Tables[0];
            if (dt_RoomWaing != null && dt_RoomWaing.Rows.Count > 0)
            {
                this.Txt_gaojikcwzyj.Text = dt_RoomWaing.Rows[0]["MinContent"].ToString();
                this.Txt_chaoxiankcwzyj.Text = dt_RoomWaing.Rows[0]["MaxContent"].ToString();
            }
            else
            {
                this.Txt_gaojikcwzyj.Text = "";
                this.Txt_chaoxiankcwzyj.Text = "";
            }
        }

        //选择原料
        protected void Tv_RawMaterial_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.Tv_RawMaterial.SelectedNode.Target == "type" || this.Tv_RawMaterial.SelectedNode.Target == "typeinfo")
            {
                this.but_waing_Save.Enabled = false;
                return;
            }

            this.but_waing_Save.Enabled = true;

            this.LoadJJX();

            //原料入库单位
            Catering_Model.Units cmru = new Catering_Rules.Units().GetModel(Convert.ToInt32(new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(this.Tv_RawMaterial.SelectedValue)).temp2));
            if (cmru != null)
            {
                this.txtMaxCMP.Text = cmru.UName;
            }
        }

        //保存警戒线
        protected void but_waing_Save_Click(object sender, EventArgs e)
        {
            string strRawID = this.Tv_RawMaterial.SelectedValue;
            string ClassID = this.DDL_banzu.SelectedValue;
            string maxInventory = this.Txt_chaoxiankcwzyj.Text.Trim();
            string minWarning = this.Txt_gaojikcwzyj.Text.Trim();

            if (string.IsNullOrEmpty(maxInventory) && string.IsNullOrEmpty(minWarning))
            {
                List<Catering_Model.RoomWaing> rw = new Catering_Rules.RoomWaing().GetModelList(" ClassID=" + ClassID + " AND RAWID=" + strRawID);
                if (rw.Count > 0)
                {
                    new Catering_Rules.RoomWaing().Delete(rw[0].id);
                    View_Operaction_Common.Show(this, "保存成功！");
                    return;
                }
            }
            else
            {
                new Catering_Rules.RoomWaing().DeleteListBywhere("RAWID=" + strRawID + " and ClassID=" + ClassID);
                Catering_Model.RoomWaing cmrw = new Catering_Model.RoomWaing();
                cmrw.id = new Catering_Rules.RoomWaing().GetMaxId();
                cmrw.GroupID = Convert.ToInt32(this.DDL_zuoyequ.SelectedValue);
                cmrw.ClassID = Convert.ToInt32(this.DDL_banzu.SelectedValue);
                cmrw.temp2 = this.txtMaxCMP.Text;
                cmrw.temp1 = this.txtMaxCMP.Text;
                cmrw.RoomID = Convert.ToInt32(this.dr_Db.SelectedValue);
                Catering_Model.E_RawMaterial cmrws = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(strRawID));
                cmrw.RAWID = Convert.ToInt32(strRawID);
                cmrw.RAWIDS = cmrws.RAWIDS;
                cmrw.RAWTID = cmrws.RAWTID;
                cmrw.MinContent = Convert.ToDecimal(minWarning == "" ? "0" : minWarning);
                cmrw.MaxContent = Convert.ToDecimal(maxInventory == "" ? "0" : maxInventory);
                new Catering_Rules.RoomWaing().Add(cmrw);
                View_Operaction_Common.Show(this, "保存成功！");
            }
        }

        private DataTable GetDBinfo(string strGroupID, string strClassID)
        {
            DataTable dt = new DataTable();
            if (strGroupID != null && strClassID != null)
            {

                DataSet ds = new Catering_Rules.StorageRoom().GetList("GroupID=" + strGroupID + "  and ClassID=" + strClassID);
                if (ds != null && ds.Tables.Count > 0)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        dt = ds.Tables[0];
                    }
                }
            }
            return dt;
        }

        //新增库房
        [WebMethod]
        public static string but_add_info(string name, string back)
        {
            Catering_Model.StorageRoom cmsr = new Catering_Model.StorageRoom();
            Catering_Rules.StorageRoom crsr = new Catering_Rules.StorageRoom();
            cmsr.id = crsr.GetMaxId();
            cmsr.RoomID = name;
            cmsr.CreateDate = DateTime.Now;
            cmsr.DBType = 1;
            cmsr.Description = back;
            cmsr.Adds = "";
            crsr.Add(cmsr);
            return "OK";
        }

        //修改库房
        [WebMethod]
        public static string but_Edit_info(string cid, string name, string back)
        {
            Catering_Model.StorageRoom cmsr = new Catering_Model.StorageRoom();
            cmsr.id = Convert.ToInt32(cid);
            cmsr.RoomID = name;
            cmsr.CreateDate = DateTime.Now;
            cmsr.DBType = 1;
            cmsr.Description = back;
            cmsr.Adds = "";
            new Catering_Rules.StorageRoom().Update(cmsr);
            return "OK";
        }

        //获取班组Option
        [WebMethod]
        public static string GetBanZuOption(string cid)
        {
            DataTable dt_banzu = new Catering_Rules.ClassInfo().GetList("AreaID=" + cid).Tables[0];
            return dt_banzu.GetOption("id", "CName");
        }

        //获取库房详细信息
        [WebMethod]
        public static ArrayList GetFuFangInfo(string cid)
        {
            ArrayList list = new ArrayList();
            Catering_Model.StorageRoom cmsr = new Catering_Rules.StorageRoom().GetModel(Convert.ToInt32(cid));
            list.Add(cmsr.RoomID);
            list.Add(cmsr.Description);
            return list;
        }

    }
}